<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="编号" width="80">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="200px" label="帖子标题">
        <template slot-scope="{row}">
          <router-link :to="'/post/audit/'+row.id" class="link-type">
            <span>{{ row.title }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column width="100px" align="center" label="作者">
        <template slot-scope="scope">
          <span>{{ scope.row.user.name}}</span>
        </template>
      </el-table-column>

      <el-table-column width="200px" label="标签">
        <template slot-scope="scope">
          <span>{{ scope.row.tag}}</span>
        </template>
      </el-table-column>

      <el-table-column width="200px" align="center" label="发布/修改日期">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ timestampToTime(scope.row.gmtModify) | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column width="110" class-name="status-col" align="center" label="状态">
        <template slot-scope="{row}">
          <el-tag :type=" chaekStatus(row.likeCount) | statusFilter">
            {{ chaekStatus(row.likeCount) }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="120">
        <template slot-scope="scope">
          <router-link :to="'/post/audit/'+scope.row.id">
            <el-button type="primary" size="small" icon="el-icon-edit">
              审核
            </el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { fetchUnAuditedList } from '@/api/post'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'ArticleList',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        通过审核: 'success',
        待审核: 'info',
        未通过审核: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchUnAuditedList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
      })
    },
    chaekStatus(status) {
      switch (status) {
        case -1 :
          return '未通过审核';
          break;
        case 0 :
          return '待审核';
          break;
        case 1 :
          return '通过审核';
          break;
      }
    },
    timestampToTime(timestamp) {
      var date = new Date(timestamp);   //创建一个指定的日期对象
      var year = date.getFullYear();  //取得4位数的年份
      var month = date.getMonth() + 1;  //取得日期中的月份，其中0表示1月，11表示12月
      var day = date.getDate();      //返回日期月份中的天数（1到31）
      var hour = date.getHours();     //返回日期中的小时数（0到23）
      var minute = date.getMinutes(); //返回日期中的分钟数（0到59）
      var second = date.getSeconds(); //返回日期中的秒数（0到59）
      if (month < 10) {
        month = '0' + month;
      }
      if (day < 10) {
        day = '0' + day;
      }
      if (hour < 10) {
        hour = '0' + hour;
      }
      if (minute < 10) {
        minute = '0' + minute;
      }
      if (second < 10) {
        second = '0' + second;
      }
      return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
    }

  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
